from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms

writer = SummaryWriter("logs")
img = Image.open('hymenoptera_data/hymenoptera_data/train/ants_image/0013035.jpg')

# ToTensor 转化为tensor类型，tensor类型是深度学习所需要的类型
trans_toTensor = transforms.ToTensor()
img_tensor = trans_toTensor(img)
writer.add_image("ToTensor", img_tensor, 2)

# Normalize  归一化
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([3, 2, 1], [1, 2, 3])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalized", img_norm, 2)

# Resize 修改尺寸
print(img.size)
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img)
# img_resize PIL =>[ toTensor ]=> img_resize tensor
img_resize = trans_toTensor(img_resize)
print(img_resize.size())
writer.add_image("Resized", img_resize, 3)

# Compose - resize - 2   流水形修改尺寸
trans_resize_2 = transforms.Resize(512)
# PIL -> PIL -> tensor
trans_compose = transforms.Compose([trans_resize_2, trans_toTensor])
img_resize_2 = trans_compose(img)
writer.add_image("Composed", img_resize_2, 0)

# RandomCrop  随机裁剪
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random, trans_toTensor])
for i in range(10):
    img_crop = trans_compose_2(img)
    writer.add_image("RandomCrop", img_crop, i)

writer.close()